package com.chuyu.cy.controller;

import com.chuyu.cy.contants.PageResult;
import com.chuyu.cy.contants.Result;
import com.chuyu.cy.entity.vo.ContactorVO;
import com.chuyu.cy.service.ContactorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 周小白
 * @date: 2024-03-16 18:45
 **/
@RestController
@RequestMapping("/contactor")
@Tag(name = "联系人管理")
public class ContactorController {

    @Resource
    private ContactorService contactorService;



    /**
     * 添加联系人
     * @param contactorAId 联系人A的id
     * @param contactorBId 联系人B的id
     * @return Result
     */
    @GetMapping("/add")
    @Operation(summary = "添加联系人")
    public Result addContactor(Integer contactorAId, Integer contactorBId) {
        Result result = new Result();

        if (contactorAId == null || contactorBId == null || contactorAId.equals(contactorBId)) {
            return result.failure("参数错误");
        }

        Boolean added = contactorService.addContactor(contactorAId, contactorBId);
        if (!added) {
            return result.failure("添加联系人失败");
        }

        return result.success();
    }






    /**
     * 获取联系人列表
     * @param userId 用户id
     * @param pageNum 页码
     * @return Result
     */
    @GetMapping("/list/{userId}")
    @Operation(summary = "获取联系人列表")
    public Result<PageResult<ContactorVO>> getContactorList(@PathVariable("userId") Integer userId,
                                                            Integer pageNum) {
        Result<PageResult<ContactorVO>> result = new Result<>();

        if (userId == null || pageNum == null) {
            return result.failure("参数错误");
        }

        PageResult<ContactorVO> contactorList = contactorService.getContactorList(userId, pageNum, 10);
        if (contactorList == null) {
            return result.failure("获取联系人列表失败");
        }

        return result.success(contactorList);
    }







    /**
     * 删除联系人
     * @param userId 用户id
     * @param contactorId 联系人id
     * @return Result
     */
    @GetMapping("/delete")
    @Operation(summary = "删除联系人")
    public Result deleteContactor(@RequestParam("userId") Integer userId,
                                  @RequestParam("contactorId") Integer contactorId) {
        Result result = new Result();

        if (userId == null || contactorId == null) {
            return result.failure("参数错误");
        }

        Boolean deleted = contactorService.deleteContactor(userId, contactorId);
        if (!deleted) {
            return result.failure("删除联系人失败");
        }

        return result.success();
    }
}
